package api

import (
	"fmt"
	"hft/global"
	"hft/tools/weixin"
	"net/url"
	"sync"

	"github.com/tidwall/gjson"
)

type Account struct{}

func (*Account) GenerateQrCode(expire_second string, action_name string, action_info string) string {

	var newStr = fmt.Sprintf(`{"action_info":{"scene":{"scene_str":"%s"}},"expire_seconds":%v,"action_name":"%v"}`, action_info, global.OC_CONFIG.WeiXin.ExpiredTime, global.OC_CONFIG.WeiXin.QRType)

	fmt.Println(newStr)

	var newD = []byte(newStr)

	data := weixin.WeiXinBase.BaseRequestV2("POST", "/cgi-bin/qrcode/create", &url.Values{}, newD)

	fmt.Println(string(data))

	errorCode := gjson.Get(string(data), "errcode").String()

	fmt.Println(errorCode)

	if errorCode != "" {
		fmt.Println(errorCode)
	}

	var url = gjson.Get(string(data), "ticket").String()

	return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + url
}

// 发送消息订阅
func (*Account) SendSubscribe(openIds []string) {

	if len(openIds) == 0 {
		return
	}

	var wg *sync.WaitGroup //上锁

	subProgramCount := len(openIds)

	wg.Add(subProgramCount)

	for i := 0; i < subProgramCount; i++ {
		go func() {

			var newD = []byte(``)

			weixin.WeiXinBase.BaseRequestV2("POST", "/cgi-bin/message/subscribe/bizsend", &url.Values{}, newD)

			wg.Done()

		}()
	}

	wg.Wait()

}

// 一次性订阅消息
func (*Account) SubOnce() {
}

var WeiXinAccount = new(Account)
